package me.caofeng.controller.manager;

import com.github.pagehelper.PageInfo;
import me.caofeng.annotation.Authentication;
import me.caofeng.enums.Role;
import me.caofeng.pojo.Page;
import me.caofeng.pojo.Product;
import me.caofeng.pojo.Response;
import me.caofeng.service.api.IDataBindService;
import me.caofeng.service.api.IProductService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.controller
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-20 20:21
 * @Version: V1.0
 */
@Controller
@RequestMapping(value = "manager/product")
@Authentication(role = Role.ADMIN)
public class ManagerProductController {

    private static Logger LOGGER = LogManager.getLogger(ManagerProductController.class);

    @Autowired
    private IProductService productService;

    @Autowired
    private IDataBindService dataBindService;

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Response<Product> addProduct(@Valid Product product, BindingResult result) {
        Response bindData = dataBindService.verifyData(result);
        if (!bindData.isSuccess()) {
            return bindData;
        }
        return productService.addProduct(product);
    }

    @RequestMapping(value = "/{pid}", method = RequestMethod.POST)
    @ResponseBody
    public Response<Product> updateProduct(@Valid Product product,
                                           @PathVariable("pid") Integer pid,
                                           BindingResult result) {
        Response bindData = dataBindService.verifyData(result);
        if (!bindData.isSuccess()) {
            return bindData;
        }

        return productService.updateProduct(product, pid);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Response<Product> getProduct(@PathVariable("id") Integer id) {
        return productService.getProduct(id,Role.ADMIN);
    }

    @RequestMapping(value = "status/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Response updateProductStatus(@PathVariable("id") Integer id,
                                        @RequestParam("status") Integer status) {
        return productService.updateProductStatus(id, status);
    }


    @RequestMapping(value = "search", method = RequestMethod.GET)
    @ResponseBody
    public Response<PageInfo<List<Product>>> getProducts(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "c", required = false) Integer category,
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "status", required = false) Integer status) {
        return productService.getProductsAsPageInfo(page, size, category, key, status, Role.ADMIN);
    }

    @RequestMapping(value = "search/{page}", method = RequestMethod.GET)
    @ResponseBody
    public Response<Page<List<Product>>> getProductsWithoutPageIndicator(
            @PathVariable("page") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "c", required = false) Integer category,
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "status", required = false) Integer status) {
        return productService.getProductsAsPage(page, size, category, key,status, Role.ADMIN);
    }

}
